package com.ymatou.iapi.couponservice.testcase;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponservice.parameter.UseCouponBean;
import com.ymatou.iapi.couponservice.parameter.invalidCouponBean;
import com.ymatou.iapi.couponservice.service.invalidCouponCall;
import com.ymatou.iapi.m.couponservice.parameter.SellerCreateBatchBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.business.service.CouponServiceCallService;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.EnvSetup;

/**
 * 作废优惠券 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_invalidCoupon {
	private static invalidCouponBean invalidcouponBean;
	private static invalidCouponCall invalidcouponCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("作废优惠券");
	}

	@Before
	public void caseUp() {
		invalidcouponBean = new invalidCouponBean();
		invalidcouponCall = new invalidCouponCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(invalidcouponCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_invalidCoupon_001() {
		Logger.start(true, "作废优惠券");
		try {

			int userId = Integer.parseInt(EnvSetup.getData("BuyUserId"));
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();

			String batchCode1 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
			couponCallService.receiveCouponInfo(batchCode1, userId);

			invalidcouponBean.setUserId(userId);
			invalidcouponBean.setBatchCode(batchCode1);
			invalidcouponBean.setInvalidMemo("自动化作废优惠券测试");
			invalidcouponBean.setAppId("m.ymatou.com");
			invalidcouponBean.setRequestId("123deddedew2323443tr34");

			invalidcouponCall.setData(invalidcouponBean);
			invalidcouponCall.callService();

			Logger.verifyEquals("操作成功！", invalidcouponCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("0", invalidcouponCall.getStatusCode1(), "验证返回StatusCode");
			
			//验证数据库
			Map batch = new YmtCouponBatchWapper().getCouponBatchByBatchCode(batchCode1).get(0);
			List<YmtCouponPrivateUserBound> privateUserBounds = new YmtCouponPrivateUserBoundWapper().selectByBatchIdAndUserId(Integer.parseInt(batch.get("iBatchId").toString()), userId);
			
			for(YmtCouponPrivateUserBound privateUserBound : privateUserBounds)
			{
				YmtCoupon coupon = new YmtCouponWapper().selectBysCouponCode(privateUserBound.getsCouponCode()).get(0);
				
				Date start = coupon.getdValidStart();
				Date enDate =coupon.getdValidEnd();
				
				Logger.verifyEquals(true, start.after(enDate), "验证数据库ymt_coupon表中这条记录ValidStart>ValidEnd");
			}			

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_invalidCoupon_002() {
		Logger.start(false, "作废优惠券-批次不存在");
		try {

			int userId = Integer.parseInt(EnvSetup.getData("BuyUserId"));
			
			invalidcouponBean.setUserId(userId);
			invalidcouponBean.setBatchCode("99999999");
			invalidcouponBean.setInvalidMemo("自动化作废优惠券测试");
			invalidcouponBean.setAppId("m.ymatou.com");
			invalidcouponBean.setRequestId("123deddedew2323443tr34");

			invalidcouponCall.setData(invalidcouponBean);
			invalidcouponCall.callService();

			Logger.verifyEquals("200106", invalidcouponCall.getStatusCode1(), "验证返回StatusCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_invalidCoupon_003() {
		Logger.start(false, "作废优惠券-未找到符合条件的用户优惠券");
		try {

			int userId = Integer.parseInt(EnvSetup.getData("BuyUserId"));
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			String batchCode1 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
			
			couponCallService.receiveCouponInfo(batchCode1, userId);

			invalidcouponBean.setUserId(12234);
			invalidcouponBean.setBatchCode(batchCode1);
			invalidcouponBean.setInvalidMemo("自动化作废优惠券测试");
			invalidcouponBean.setAppId("m.ymatou.com");
			invalidcouponBean.setRequestId("123deddedew2323443tr34");

			invalidcouponCall.setData(invalidcouponBean);
			invalidcouponCall.callService();

			Logger.verifyEquals("21002", invalidcouponCall.getStatusCode1(), "验证返回StatusCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_invalidCoupon_004() {
		Logger.start(false, "作废优惠券-用户优惠券已被使用");
		try {

			int userId = Integer.parseInt(EnvSetup.getData("BuyUserId"));
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			String batchCode1 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
			
			String couponCode = couponCallService.receiveCouponInfo(batchCode1, userId).getUserReceiveResult().getCouponCode();
		
			CouponServiceCallService couponServiceCallService = new CouponServiceCallService();	
			
			UseCouponBean useCouponBean = couponServiceCallService.setUseCouponBean(userId, 12233, couponCode);
			couponServiceCallService.useCoupon(useCouponBean);

			invalidcouponBean.setUserId(userId);
			invalidcouponBean.setBatchCode(batchCode1);
			invalidcouponBean.setInvalidMemo("自动化作废优惠券测试");
			invalidcouponBean.setAppId("m.ymatou.com");
			invalidcouponBean.setRequestId("123deddedew2323443tr34");

			invalidcouponCall.setData(invalidcouponBean);
			invalidcouponCall.callService();

			Logger.verifyEquals("21001", invalidcouponCall.getStatusCode1(), "验证返回StatusCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_invalidCoupon_005() {
		Logger.start(false, "作废优惠券-UserId空");
		try {

			int userId = Integer.parseInt(EnvSetup.getData("BuyUserId"));
			
			invalidcouponBean.setBatchCode("99999999");
			invalidcouponBean.setInvalidMemo("自动化作废优惠券测试");
			invalidcouponBean.setAppId("m.ymatou.com");
			invalidcouponBean.setRequestId("123deddedew2323443tr34");

			invalidcouponCall.setData(invalidcouponBean);
			invalidcouponCall.callService();

			Logger.verifyEquals("1", invalidcouponCall.getStatusCode1(), "验证返回StatusCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_invalidCoupon_006() {
		Logger.start(false, "作废优惠券-BatchCode空");
		try {

			int userId = Integer.parseInt(EnvSetup.getData("BuyUserId"));
			
			invalidcouponBean.setUserId(999999);
			invalidcouponBean.setInvalidMemo("自动化作废优惠券测试");
			invalidcouponBean.setAppId("m.ymatou.com");
			invalidcouponBean.setRequestId("123deddedew2323443tr34");

			invalidcouponCall.setData(invalidcouponBean);
			invalidcouponCall.callService();

			Logger.verifyEquals("1", invalidcouponCall.getStatusCode1(), "验证返回StatusCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_invalidCoupon_007() {
		Logger.start(true, "作废已作废优惠券");
		try {

			int userId = Integer.parseInt(EnvSetup.getData("BuyUserId"));
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			String batchCode1 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();	couponCallService.receiveCouponInfo(batchCode1, userId);

			invalidcouponBean.setUserId(userId);
			invalidcouponBean.setBatchCode(batchCode1);
			invalidcouponBean.setInvalidMemo("自动化作废优惠券测试");
			invalidcouponBean.setAppId("m.ymatou.com");
			invalidcouponBean.setRequestId("123deddedew2323443tr34");

			invalidcouponCall.setData(invalidcouponBean);
			invalidcouponCall.callService();
			
			invalidcouponCall.setData(invalidcouponBean);
			invalidcouponCall.callService();


			Logger.verifyEquals("操作成功！", invalidcouponCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("0", invalidcouponCall.getStatusCode1(), "验证返回StatusCode");
			
			//验证数据库
			Map batch = new YmtCouponBatchWapper().getCouponBatchByBatchCode(batchCode1).get(0);
			List<YmtCouponPrivateUserBound> privateUserBounds = new YmtCouponPrivateUserBoundWapper().selectByBatchIdAndUserId(Integer.parseInt(batch.get("iBatchId").toString()), userId);
			
			for(YmtCouponPrivateUserBound privateUserBound : privateUserBounds)
			{
				YmtCoupon coupon = new YmtCouponWapper().selectBysCouponCode(privateUserBound.getsCouponCode()).get(0);
				
				Date start = coupon.getdValidStart();
				Date enDate =coupon.getdValidEnd();
				
				Logger.verifyEquals(true, start.after(enDate), "验证数据库ymt_coupon表中这条记录ValidStart>ValidEnd");
			}			

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}